home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-19 / iritsm3s.zip / ATTRIBUT.C < prev    next >
C/C++ Source or Header  |  1992-02-29  |  6KB  |  169 lines

  1. /*****************************************************************************
  2. *   "Irit" - the 3d polygonal solid modeller.                     *
  3. *                                         *
  4. * Written by:  Gershon Elber                Ver 0.2, Mar. 1990   *
  5. ******************************************************************************
  6. * Setting attributes for geometric objects.                     *
  7. *****************************************************************************/
  8.  
  9. #include <string.h>
  10. #include <stdio.h>
  11. #include <math.h>
  12. #include "program.h"
  13. #include "allocate.h"
  14. #include "attribut.h"
  15. #include "windows.h"
  16. #include "graphgen.h"
  17.  
  18. /*****************************************************************************
  19. *   Routine to return the color of the geometric object.             *
  20. *****************************************************************************/
  21. int GetObjectColor(ObjectStruct *PObj)
  22. {
  23.     if (!IS_GEOM_OBJ(PObj))
  24.     FatalError("Attributes are for geometry objects only");
  25.  
  26.     return (int) (PObj -> U.Attr.Color);
  27. }
  28.  
  29. /*****************************************************************************
  30. *   Routine to set the color of the geometric object.                 *
  31. *****************************************************************************/
  32. void SetObjectColor(ObjectStruct *PObj, int Color)
  33. {
  34.     if (!IS_GEOM_OBJ(PObj))
  35.     FatalError("Attributes are for geometry objects only");
  36.  
  37.     PObj -> U.Attr.Color = (ByteType) Color;
  38. }
  39.  
  40. /*****************************************************************************
  41. *   Routine to set a string attribute. A string attribute consists of an     *
  42. * attribute name (string) and data (also string).                 *
  43. *   If Name = "", all attributes of given object are printed.             *
  44. *   If Data = "", the attribute with name Name is been freed.             *
  45. *   If attribute by the given name already exists, it is replaced.         *
  46. *****************************************************************************/
  47. void SetObjectStrAttrib(ObjectStruct *PObj, char *Name, char *Data)
  48. {
  49.     int i;
  50.     char Line[LINE_LEN_LONG];
  51.     AttributeStruct *Attr;
  52.  
  53.     if (!IS_GEOM_OBJ(PObj))
  54.     FatalError("Attributes are for geometry objects only");
  55.  
  56.     Attr = &(PObj -> U.Attr);
  57.  
  58.     /* If Name is an empty string - print all attributes. */
  59.     if (strlen(Name) == 0) {
  60.     sprintf(Line, "[COLOR %d]", Attr -> Color);
  61.     WndwInputWindowPutStr(Line);
  62.     for (i = 0; i < Attr -> NumStrAttribs; i++) {
  63.         sprintf(Line, "[%s %s]",
  64.             Attr -> StrAttrName[i], Attr -> StrAttrData[i]);
  65.         WndwInputWindowPutStr(Line);
  66.     }
  67.     return;
  68.     }
  69.  
  70.     for (i = 0; i < Attr -> NumStrAttribs; i++) {
  71.     if (strcmp(Name, Attr -> StrAttrName[i]) == 0) {
  72.         /* If Data is an empty string, remove this entry. */
  73.         if (strlen(Data) == 0) {
  74.         MyFree((VoidPtr) Attr -> StrAttrName[i], ALLOC_OTHER);
  75.         MyFree((VoidPtr) Attr -> StrAttrData[i], ALLOC_OTHER);
  76.         for ( ; i < (int) Attr -> NumStrAttribs - 2; i++) {
  77.             Attr -> StrAttrName[i] = Attr -> StrAttrName[i + 1];
  78.             Attr -> StrAttrData[i] = Attr -> StrAttrData[i + 1];
  79.         }
  80.         Attr -> NumStrAttribs--;
  81.         }
  82.         else {
  83.         MyFree((VoidPtr) Attr -> StrAttrData[i], ALLOC_OTHER);
  84.         Attr -> StrAttrData[i] = strdup(Data);
  85.         }
  86.  
  87.         return;
  88.     }
  89.     }
  90.  
  91.     /* O.k. it is a new attribute. */
  92.     if (Attr -> NumStrAttribs >= MAX_NUM_ATTRS) {
  93.     WndwInputWindowPutStr("Maximum num. of attributes exceeded");
  94.     return;
  95.     }
  96.     Attr -> StrAttrName[Attr -> NumStrAttribs] = strdup(Name);
  97.     Attr -> StrAttrData[Attr -> NumStrAttribs++] = strdup(Data);
  98. }
  99.  
  100. /*****************************************************************************
  101. *   Routine to get a string attribute. A string attribute consists of an     *
  102. * attribute name (string) and data (also string).                 *
  103. *   Returns a pointer to data if string name is found, NULL otherwise.         *
  104. *****************************************************************************/
  105. char *GetObjectStrAttrib(ObjectStruct *PObj, char *Name)
  106. {
  107.     int i;
  108.     AttributeStruct *Attr;
  109.  
  110.     if (!IS_GEOM_OBJ(PObj))
  111.     FatalError("Attributes are for geometry objects only");
  112.  
  113.     Attr = &(PObj -> U.Attr);
  114.  
  115.     /* If Name is an empty string - print all attributes. */
  116.     if (Name == NULL || strlen(Name) == 0) return NULL;
  117.  
  118.     for (i = 0; i < Attr -> NumStrAttribs; i++)
  119.     if (strcmp(Name, Attr -> StrAttrName[i]) == 0)
  120.             return Attr -> StrAttrData[i];
  121.  
  122.     return NULL;
  123. }
  124.  
  125. /*****************************************************************************
  126. *   Routine to release all attributes of the given Object.             *
  127. *****************************************************************************/
  128. void ReleaseStrAttrib(ObjectStruct *PObj)
  129. {
  130.     int i;
  131.     AttributeStruct *Attr;
  132.  
  133.     if (!IS_GEOM_OBJ(PObj))
  134.     FatalError("Attributes are for geometry objects only");
  135.  
  136.     Attr = &(PObj -> U.Attr);
  137.     for (i = 0; i < Attr -> NumStrAttribs; i++) {
  138.     MyFree((VoidPtr) Attr -> StrAttrName[i], ALLOC_OTHER);
  139.     MyFree((VoidPtr) Attr -> StrAttrData[i], ALLOC_OTHER);
  140.     }
  141. }
  142.  
  143. /*****************************************************************************
  144. *   Routine to initialize the Attributes of the given Object.             *
  145. *****************************************************************************/
  146. void ResetObjectAttribs(ObjectStruct *PObj)
  147. {
  148.     SetObjectColor(PObj, WHITE);         /* Default color attribute. */
  149.     PObj -> U.Attr.NumStrAttribs = 0;
  150. }
  151.  
  152. /*****************************************************************************
  153. *   Routine to copy the attributes from one object to another.             *
  154. *****************************************************************************/
  155. void CopyGeomAttrib(ObjectStruct *Dest, ObjectStruct *Src)
  156. {
  157.     int i;
  158.     if (!IS_GEOM_OBJ(Dest) || !IS_GEOM_OBJ(Src))
  159.     FatalError("Attributes are for geometry objects only");
  160.  
  161.     Dest -> U.Attr.Color = Src -> U.Attr.Color;
  162.     Dest -> U.Attr.NumStrAttribs = Src -> U.Attr.NumStrAttribs;
  163.  
  164.     for (i = 0; i < (Dest -> U.Attr.NumStrAttribs); i++) {
  165.     Dest -> U.Attr.StrAttrName[i] = strdup(Src -> U.Attr.StrAttrName[i]);
  166.     Dest -> U.Attr.StrAttrData[i] = strdup(Src -> U.Attr.StrAttrData[i]);
  167.     }
  168. }
  169.